<template>
  <div id="new-flight-order">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb">
        <li class="breadcrumb-item">
          <router-link to="/h5/flt/orders">机票订单</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">新建</li>
        <span class="ms-auto" @click.stop="back()">返回</span>
      </ol>
    </nav>
    <div class="card" v-if="editing === false">
      <div class="card-body px-1 py-1">
        <div class="row">
          <label class="col-3 text-end">导入方式</label>
          <div class="col-6">
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="0"
                v-model.number="pnrMode"
              />
              <label class="form-check-label">编码内容</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="1"
                v-model.number="pnrMode"
              />
              <label class="form-check-label">编码</label>
            </div>
          </div>
        </div>
        <div class="row">
          <template v-if="pnrMode === 0">
            <textarea
              class="form-control"
              rows="12"
              v-model.trim="pnrDetail"
              placeholder="请将编码内容粘帖到此处"
            ></textarea>
          </template>
          <template v-else-if="pnrMode === 1">
            <textarea
              class="form-control"
              rows="1"
              v-model.trim="pnrDetail"
              placeholder="请将编码粘帖到此处"
            ></textarea>
            提示：<span class="text-warning"
              >使用编码导入订单需要后台接口支持。</span
            >
            <br />
          </template>
          <button
            type="button"
            class="btn btn-primary btn-sm mt-1"
            :disabled="btnDisabled"
            @click.stop="processPnrDetail()"
          >
            <template v-if="pnrMode === 0"> 导入编码内容 </template>
            <template v-else-if="pnrMode === 1"> 导入编码 </template>
          </button>
          <button
            type="button"
            class="btn btn-info btn-sm mt-1 float-end"
            @click.stop="inputPnr()"
          >
            手工录入
          </button>
        </div>
      </div>
    </div>
    <template v-if="pnr !== null && editing">
      <div class="card">
        <div class="card-body bg-light py-1">
          航段
          <a
            href="javascript:void(0);"
            @click.stop="addPnrFlt()"
            class="btn btn-sm btn-success"
            >增加</a
          >
        </div>

        <template v-for="(info, index) in pnr.flights">
          <div class="card-body" :key="index">
            第{{ index + 1 }}段
            <dl class="row">
              <dt class="col-4 text-end">航班号</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="10"
                  placeholder="航班号"
                  v-model.trim="info.flight.flightNo"
                />
              </dd>
              <dt class="col-4 text-end">起飞日期</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="15"
                  placeholder="示例：2020-01-09"
                  v-model.trim="info.flight.ddate"
                />
              </dd>
              <dt class="col-4 text-end">舱位</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="2"
                  placeholder="舱位"
                  v-model.trim="info.flight.subclass"
                />
              </dd>
              <dt class="col-4 text-end">出发机场</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="3"
                  placeholder="出发机场"
                  v-model.trim="info.flight.dport"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end">出发航站楼</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="3"
                  placeholder="出发航站楼"
                  v-model.trim="info.flight.dterm"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end">到达机场</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="3"
                  placeholder="到达机场"
                  v-model.trim="info.flight.aport"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end">到达航站楼</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  maxlength="3"
                  placeholder="到达航站楼"
                  v-model.trim="info.flight.aterm"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end">起飞时间</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  placeholder="示例：1800"
                  v-model.trim="info.flight.dtime"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end">到达时间</dt>
              <dd class="col-8">
                <input
                  type="text"
                  class="form-control"
                  placeholder="示例：2100"
                  v-model.trim="info.flight.atime"
                  size="10"
                />
              </dd>
              <dt class="col-4 text-end"></dt>
              <dd class="col-8">
                <a
                  href="javascript:void(0)"
                  @click.stop="deletePnrFlt(index)"
                  class="btn btn-sm btn-danger"
                  >删除</a
                >
              </dd>
            </dl>
          </div>
        </template>

        <div class="card-body bg-light py-1 sticky-top">
          乘机人
          <a
            href="javascript:void(0);"
            @click.stop="addPnrPsg()"
            class="btn btn-sm btn-success"
            >增加</a
          >
        </div>
        <div class="card-body">
          <a
            href="javascript:void(0)"
            @click.stop="selectAllPsg()"
            class="ms-auto me-auto"
            >全选</a
          >
          <a
            href="javascript:void(0)"
            @click.stop="unselectAllPsg()"
            class="ms-auto me-auto"
            >全否</a
          >
        </div>
        <div class="card-body">
          <dl class="row" v-for="(info, index) in pnr.passengers" :key="index">
            <dt class="col-4 text-end">姓名</dt>
            <dd class="col-8">
              <input
                type="text"
                class="form-control"
                placeholder="乘机人姓名"
                v-model.trim="info.psgName"
              />
            </dd>
            <dt class="col-4 text-end">证件号</dt>
            <dd class="col-8">
              <input
                type="text"
                class="form-control"
                placeholder="证件号"
                v-model.trim="info.idNo"
              />
            </dd>
            <dt class="col-4 text-end">证件类型</dt>
            <dd class="col-8">
              <select
                class="form-control"
                v-model.number="info.idType"
                placeholder="证件类型"
              >
                <option value="1">身份证</option>
                <option value="2">护照</option>
                <option value="99">其它</option>
              </select>
            </dd>
            <dt class="col-4 text-end">常旅客号</dt>
            <dd class="col-8">
              <input
                type="text"
                class="form-control"
                placeholder="常旅客号"
                v-model.trim="info.ffpNo"
              />
            </dd>
            <dt class="col-4 text-end">手机</dt>
            <dd class="col-8">
              <input
                type="text"
                class="form-control"
                placeholder="手机号"
                v-model.trim="info.mobile"
              />
            </dd>
            <dt class="col-4 text-end">乘客类型</dt>
            <dd class="col-8">
              <select
                class="form-control"
                v-model.number="info.psgType"
                placeholder
              >
                <option value="0">成人</option>
                <option value="1">儿童</option>
                <option value="2">婴儿</option>
              </select>
            </dd>
            <dt class="col-4 text-end">是否保存</dt>
            <dd class="col-8">
              <input type="checkbox" v-model="info.selected" />
            </dd>

            <dt class="col-4 text-end"></dt>
            <dd class="col-8">
              <a
                href="javascript:void(0)"
                @click.stop="deletePnrPsg(index)"
                class="btn btn-sm btn-danger float-end"
                >删除</a
              >
            </dd>
          </dl>
        </div>
        <div class="card-body bg-info text-white py-0">结算信息</div>
        <div class="card-body">
          <my-price-input
            psgTypeName="成人"
            v-bind.sync="adultPrice"
            key="adultPriceInput"
            v-if="adultCount > 0"
          ></my-price-input>
          <my-price-input
            psgTypeName="儿童"
            v-bind.sync="childPrice"
            key="childPriceInput"
            v-if="childCount > 0"
          ></my-price-input>
          <my-price-input
            psgTypeName="婴儿"
            v-bind.sync="infantPrice"
            key="infantPriceInput"
            v-if="infantCount > 0"
          ></my-price-input>
        </div>
        <table class="table table-sm">
          <thead>
            <tr class="bg-info text-white">
              <td>总计</td>
              <td>人数</td>
              <td>总应收</td>
              <td>总应付</td>
              <td>利润</td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td></td>
              <td>{{ psgCount }}</td>
              <td>{{ totalAmount }}</td>
              <td>{{ totalCost }}</td>
              <td>
                <span class="text-success">{{ totalProfit }}</span>
              </td>
            </tr>
          </tbody>
        </table>
        <div class="card-body bg-info text-white py-0">客户及付款信息</div>
        <div class="card-body">
          <dl class="row mt-2">
            <dt class="col-3 text-end">客户</dt>
            <dd class="col-9">
              <my-select-customer
                :status="1"
                :minId="0"
                :customerId.sync="customerId"
              ></my-select-customer>
            </dd>
            <template v-if="costCenters.length > 0">
              <dt class="col-3 text-end">成本中心</dt>
              <dd class="col-9">
                <select class="form-select" v-model="costCenter">
                  <option value>请选择</option>
                  <option
                    :value="info.name"
                    v-for="info in costCenters"
                    :key="info.id"
                  >
                    {{ info.name }}
                  </option>
                </select>
              </dd>
            </template>
            <template v-if="projectNames.length > 0">
              <dt class="col-3 text-end">项目名称</dt>
              <dd class="col-9">
                <select class="form-select" v-model="projectName">
                  <option value>请选择</option>
                  <option
                    :value="info.name"
                    v-for="info in projectNames"
                    :key="info.id"
                  >
                    {{ info.name }}
                  </option>
                </select>
              </dd>
            </template>

            <dt class="col-3 text-end">付款方式</dt>
            <dd class="col-9">
              <div class="form-check form-check-inline">
                <input
                  class="form-check-input"
                  type="radio"
                  value="1"
                  v-model.number="payType"
                />
                <label class="form-check-label">现付（现金等）</label>
              </div>
              <div class="form-check form-check-inline">
                <input
                  class="form-check-input"
                  type="radio"
                  value="8"
                  v-model.number="payType"
                />
                <label class="form-check-label">记账</label>
              </div>
            </dd>
            <template v-if="payType === 1">
              <dt class="col-3 text-end">付款备注</dt>
              <dd class="col-9">
                <my-select-income
                  :incomeRemark.sync="payRemark"
                ></my-select-income>
              </dd>
            </template>
          </dl>
        </div>
        <div class="card-body bg-info text-white py-0">行程单/发票</div>
        <div class="card-body">
          <div class="form-check form-check-inline">
            <input
              class="form-check-input"
              type="radio"
              value="0"
              v-model.number="itineraryType"
            />
            <label class="form-check-label">不需要</label>
          </div>
          <div class="form-check form-check-inline">
            <input
              class="form-check-input"
              type="radio"
              value="1"
              v-model.number="itineraryType"
            />
            <label class="form-check-label">行程单</label>
          </div>
          <div class="form-check form-check-inline">
            <input
              class="form-check-input"
              type="radio"
              value="2"
              v-model.number="itineraryType"
            />
            <label class="form-check-label">发票</label>
          </div>
          <div class="form-check form-check-inline">
            <input
              class="form-check-input"
              type="radio"
              value="3"
              v-model.number="itineraryType"
            />
            <label class="form-check-label">信息单</label>
          </div>
        </div>
        <template v-if="itineraryType > 0">
          <dl class="row">
            <dt class="col-3 text-end">派送日期</dt>
            <dd class="col-9">
              <my-date-picker
                id="deliveryDate"
                v-model.trim="deliveryDate"
              ></my-date-picker>
            </dd>
            <dt class="col-3 text-end">邮寄地址</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                v-model.trim="itineraryMailingAddress"
              />
            </dd>
            <dt class="col-3 text-end">收件人</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                v-model.trim="itineraryRecipient"
              />
            </dd>
            <dt class="col-3 text-end">联系电话</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                v-model.trim="itineraryRecipientMobile"
              />
            </dd>
            <dt class="col-3 text-end">备注</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                v-model.trim="itineraryMailingMemo"
              />
            </dd>
          </dl>
        </template>
        <div class="card-body bg-info text-white py-1">订票人</div>
        <div class="card-body">
          <dl class="row">
            <dt class="col-3 text-end">姓名</dt>
            <dd class="col-9">
              <input type="text" class="form-control" v-model.trim="linkman" />
            </dd>
            <dt class="col-3 text-end">电话</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                v-model.trim="linkPhone"
              />
            </dd>
          </dl>
        </div>
        <div class="card-body bg-info text-white py-1">
          供应商及支出付款方式
        </div>
        <div class="card-body">
          <dl class="row">
            <dt class="col-3 text-end">供应商</dt>
            <dd class="col-9">
              <my-select-supplier
                :supplierId.sync="supplierId"
              ></my-select-supplier>
            </dd>
            <dt class="col-3 text-end">付款方式</dt>
            <dd class="col-9">
              <my-select-payment
                :paymentId.sync="paymentMethodId"
              ></my-select-payment>
            </dd>
          </dl>
        </div>
        <div class="card-body bg-info text-white py-1">其它</div>
        <div class="card-body">
          <dl class="row">
            <dt class="col-3 text-end">时限</dt>
            <dd class="col-9">
              <my-date-picker
                id="etdzDate"
                v-model.trim="etdzDate"
              ></my-date-picker>
              <div class="text-muted">最晚开票日期</div>
              <input
                type="text"
                class="form-control"
                v-model.trim="etdzTime"
                placeholder="时间：2330"
              />
            </dd>
            <dt class="col-3 text-end">备注</dt>
            <dd class="col-9">
              <input type="text" class="form-control" v-model.trim="memo" />
            </dd>
          </dl>
        </div>
        <div class="card-body bg-info text-white py-1">预订人</div>
        <div class="card-body">
          <dl class="row">
            <dt class="col-3 text-end">预订人</dt>
            <dd class="col-9">
              <my-select-op1 sizing="sm" :username.sync="op1">
                <span slot="title">本人</span>
              </my-select-op1>
            </dd>
          </dl>
        </div>
        <div class="card-body">
          <div class="offset-2">
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="0"
                v-model.number="intlTicket"
              />
              <label class="form-check-label">国内航班</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="1"
                v-model.number="intlTicket"
              />
              <label class="form-check-label">国际航班</label>
            </div>
          </div>
        </div>

        <div class="card-footer text-center">
          <button
            type="button"
            class="btn btn-primary"
            :disabled="btnDisabled"
            @click.stop.prevent="createFlightOrder()"
          >
            保存新订单
          </button>
          <span>保存{{ psgCount }}人, 共{{ psgTotal }}人</span>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import {
  processPnrDetail,
  searchPriceByRoute,
  searchFlightFixedCommission,
} from "@/api/user.js";
import { searchCustomers } from "@/api/customer.js";
import { createFlightOrder } from "@/api/flight.js";
import { processPnrFlightContent } from "@/api/flight-change.js";
import { searchNotesByCustomer } from "@/api/customer.js";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyPriceInput from "@/components/flight-price-input_h5.vue";
import MySelect2 from "@/components/my-select2.vue";
import MySelectCustomer from "@/components/my-select-customer.vue";
import MySelectIncome from "@/components/my-select-income.vue";
import PriceInfo from "@/common/PriceInfo.js";
import MySelectPayment from "@/components/my-select-payment.vue";
import MySelectSupplier from "@/components/my-select-supplier.vue";
import MySelectOp1 from "@/components/my-select-operator.vue";
import $ from "jquery";

export default {
  components: {
    MyDatePicker,
    MyPriceInput,
    MySelect2,
    MySelectCustomer,
    MySelectIncome,
    MySelectPayment,
    MySelectSupplier,
    MySelectOp1,
  },
  data() {
    return {
      editing: false,
      pnrMode: 0,
      pnrDetail: "",
      pnr: null,

      customerId: 0,
      costCenter: "",
      projectName: "",
      costCenters: [],
      projectNames: [],

      supplierId: 0,
      paymentMethodId: 0,

      adultPrice: new PriceInfo(),
      childPrice: new PriceInfo(),
      infantPrice: new PriceInfo(),

      adultCount: 0,
      childCount: 0,
      infantCount: 0,

      memo: "",
      etdzDate: "",
      etdzTime: "",
      payType: 1,
      payRemark: "",
      itineraryType: 0,
      itineraryMailingAddress: "",
      itineraryMailingMemo: "",
      itineraryRecipient: "",
      itineraryRecipientMobile: "",
      deliveryDate: "",
      linkman: "",
      linkPhone: "",
      intlTicket: -1,
      op1: "",

      inputMode: 0,
      quickInput: "",
      quickInputIndex: 0,

      btnDisabled: false,
    };
  },
  watch: {
    customerId: function (newVal) {
      console.log("customerId:" + newVal);
      if (newVal === 0) {
        this.payType = 1;
      } else if (newVal > 0) {
        this.payType = 8;
      }
      this.getMailAddress();
      this.getCostCenters();
    },
    quickInput: function (newVal, oldVal) {
      this.processQuickInput();
    },
  },
  computed: {
    flightCount: function () {
      if (this.pnr === null) {
        return 0;
      }

      return this.pnr.flights.length;
    },
    psgCount: function () {
      if (this.pnr === null) {
        return 0;
      }

      let count = 0;
      this.adultCount = 0;
      this.childCount = 0;
      this.infantCount = 0;

      for (const psg of this.pnr.passengers) {
        if (psg.selected === false) continue;

        count++;

        if (psg.psgType === 1) {
          this.childCount++;
        } else if (psg.psgType === 2) {
          this.infantCount++;
        }
      }

      this.adultCount = count - this.childCount - this.infantCount;

      this.adultPrice.ticketCount = this.adultCount;
      this.childPrice.ticketCount = this.childCount;
      this.infantPrice.ticketCount = this.infantCount;

      return count;
    },
    psgTotal: function () {
      if (this.pnr === null) {
        return 0;
      }
      return this.pnr.passengers.length;
    },
    totalAmount: function () {
      return (
        this.adultPrice.amount * this.adultCount +
        this.childPrice.amount * this.childCount +
        this.infantPrice.amount * this.infantCount
      );
    },
    totalCost: function () {
      return (
        this.adultPrice.cost * this.adultCount +
        this.childPrice.cost * this.childCount +
        this.infantPrice.cost * this.infantCount
      );
    },
    totalProfit: function () {
      return (
        this.adultPrice.profit * this.adultCount +
        this.childPrice.profit * this.childCount +
        this.infantPrice.profit * this.infantCount
      );
    },
  },
  mounted: function () {
    $('[data-bs-toggle="tooltip"]').tooltip();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    processPnrDetail: function () {
      if (this.pnrDetail.length === 0) {
        this.showErrMsg("编码内容不能为空");
        return;
      }
      this.btnDisabled = true;
      const params = { pnrDetail: this.pnrDetail };

      this.adultCount = 0;
      this.childCount = 0;

      processPnrDetail(params, (jsonResult) => {
        this.btnDisabled = false;
        this.inputMode = 0;
        this.pnr = jsonResult;
        this.etdzDate = this.pnr.tktlDate;
        this.etdzTime = this.pnr.tktlTime;
        this.adultPrice.price = this.pnr.price;
        this.adultPrice.parValue = this.pnr.parValue;
        this.adultPrice.tax = this.pnr.tax;
        this.adultPrice.commRate = this.pnr.commRate;
        this.adultCount = this.pnr.adultCount;

        if (this.pnr.childCount > 0) {
          this.childCount = this.pnr.childCount;
          this.childPrice.price = this.pnr.chdPrice;
          this.childPrice.parValue = this.pnr.chdParValue;
          this.childPrice.tax = this.pnr.chdTax;
          this.childPrice.commRate = this.pnr.chdCommRate;
        }

        if (this.pnr.infantCount > 0) {
          this.infantCount = this.pnr.infantCount;
          this.infantPrice.price = this.pnr.infPrice;
          this.infantPrice.parValue = this.pnr.infParValue;
          this.infantPrice.tax = this.pnr.infTax;
          this.infantPrice.commRate = this.pnr.infCommRate;
        }

        this.editing = true;
      });
    },
    searchPriceByPnr: function (carrier, subclass, dport, aport) {
      const params = {
        "sc.carrier": carrier,
        "sc.subClass": subclass,
        "sc.dport": dport,
        "sc.aport": aport,
      };

      searchPriceByRoute(params, (jsonResult) => {
        this.price = 0;
        if (jsonResult.dataList.length > 0) {
          this.adultPrice.price = jsonResult.dataList[0].price;
          this.caclTicketAmount();
        }
      });
    },
    createFlightOrder: function () {
      console.log("createFlightOrder");
      if (this.intlTicket !== 0 && this.intlTicket !== 1) {
        this.showErrMsg("请确定订单为国内订单，还是国际订单", "danger");
        return;
      }

      const params = {
        customerId: this.customerId,
        costCenter: this.costCenter,
        projectName: this.projectName,
        payType: this.payType,
        payRemark: this.payRemark,

        adultPrice: this.adultPrice,
        childPrice: this.childPrice,
        infantPrice: this.infantPrice,

        totalAmount: this.totalAmount,
        totalProfit: this.totalProfit,

        tktlDate: this.etdzDate,
        tktlTime: this.etdzTime,
        pnrNo: this.pnr.pnrNo,
        pnrDetail: this.pnrDetail,
        itineraryType: this.itineraryType,
        itineraryMailingAddress: this.itineraryMailingAddress,
        itineraryMailingMemo: this.itineraryMailingMemo,
        itineraryRecipient: this.itineraryRecipient,
        itineraryRecipientMobile: this.itineraryRecipientMobile,
        deliveryDate: this.deliveryDate,
        linkman: this.linkman,
        linkPhone: this.linkPhone,
        intlTicket: this.intlTicket,
        flights: this.pnr.flights,
        passengers: this.pnr.passengers,

        supplierId: this.supplierId,
        paymentMethodId: this.paymentMethodId,
        op1: this.op1,
      };

      const jsonParams = JSON.stringify(params);

      this.showLoading("保存中");

      createFlightOrder(
        jsonParams,
        (jsonResult) => {
          if (jsonResult.status !== "OK") {
            this.showErrMsg(jsonResult.errmsg, "danger");
          } else {
            // 根据条件判断是否跳转，还是停在此页
            if (this.psgCount === this.psgTotal) {
              this.showErrMsg("全部保存成功，跳转到订单详情页面");
              this.pnr = null;
              this.pnrDetail = "";
              this.$router.push("/h5/flt/order/" + jsonResult.returnCode);
            } else {
              // 留在当前页面，继续保存
              this.showErrMsg("保存成功，请继续保存余下的乘机人");
              let b = true;
              while (b) {
                b = false;
                for (let i = 0; i < this.pnr.passengers.length; i++) {
                  if (this.pnr.passengers[i].selected) {
                    b = true;
                    this.deletePnrPsg(i);
                  }
                }
              }
            }
          }
        },
        () => {
          this.hideLoading();
        }
      );
    },
    deletePnrTicket: function (index) {
      this.pnr.tickets.splice(index, 1);
    },
    deletePnrPsg: function (index) {
      this.pnr.passengers.splice(index, 1);
    },
    deletePnrFlt: function (index) {
      this.pnr.flights.splice(index, 1);
    },
    inputPnr: function () {
      this.inputMode = 1;
      this.pnr = {
        flights: [],
        passengers: [],
      };
      this.addPnrFlt();
      this.addPnrPsg();

      this.editing = true;
    },
    addPnrFlt: function () {
      this.pnr.flights.push({
        flight: {
          flightNo: "",
          departureDate: "",
          subclass: "",
          departureAirport: "",
          departureTerminal: "",
          arrivalAirport: "",
          arrivalTerminal: "",
          departureTime: "",
          arrivalTime: "",
        },
      });
    },
    addPnrPsg: function () {
      this.pnr.passengers.push({
        selected: true,
        psgName: "",
        idNo: "",
        idType: "",
        mobile: "",
      });
    },
    reset: function () {
      // body...
      this.pnrDetail = "";
      this.pnr = null;

      this.customerId = -1;

      this.adultPrice.reset();
      this.childPrice.reset();
      this.infantPrice.reset();

      this.etdzMemo = "";
      this.payType = 8;

      this.inputMode = 0;
    },
    getMailAddress: function () {
      this.itineraryMailingAddress = "";
      this.itineraryType = 0;
      this.itineraryRecipient = "";
      this.itineraryRecipientMobile = "";

      if (this.customerId < 1) return;

      const params = {
        "sc.pageNo": 1,
        "sc.pageSize": 1,
        "sc.customerId": this.customerId,
      };

      searchCustomers(params, (jsonResult) => {
        const customers = jsonResult.dataList;

        for (let i = 0; i < customers.length; i++) {
          if (customers[i].id === this.customerId) {
            // console.log(customers[i])
            this.itineraryMailingAddress = customers[i].address;
            this.itineraryType = customers[i].itineraryType;
            this.itineraryRecipient = customers[i].linkman;
            this.itineraryRecipientMobile = customers[i].phone;
          }
        }
      });
    },
    getCostCenters: function () {
      this.costCenter = "";
      this.projectName = "";
      this.costCenters.splice(0);
      this.projectNames.splice(0);
      if (this.customerId < 1) return;

      searchNotesByCustomer(this.customerId, (jsonResult) => {
        for (const info of jsonResult) {
          if (info.noteType === 100) {
            this.costCenters.push(info);
          } else if (info.noteType === 200) {
            this.projectNames.push(info);
          }
        }
      });
    },
    processQuickInput: function () {
      if (
        this.flightCount < 1 ||
        this.quickInputIndex < 0 ||
        this.quickInputIndex >= this.flightCount
      ) {
        return;
      }
      if (this.quickInput.length < 6) {
        return;
      }

      const idx = this.quickInput.indexOf(".");
      if (idx > 0) {
        // pnr中的航班格式
        processPnrFlightContent({ content: this.quickInput }, (v) => {
          if (v !== null && v.flight !== null) {
            // console.log(v)
            this.quickInput = "";
            const flt = this.pnr.flights[this.quickInputIndex].flight;

            flt.departureAirport = v.flight.departureAirport;
            flt.arrivalAirport = v.flight.arrivalAirport;
            flt.flightNo = v.flight.flightNo;
            flt.subclass = v.flight.subclass;
            flt.departureDate = v.flight.departureDate;
            flt.departureTime = v.flight.departureTime;
            flt.arrivalTime = v.flight.arrivalTime;
          }
        });
      } else {
        // 自定义的格式
        const items = this.quickInput.split("/");
        // console.log(items)
        if (items.length > 0) {
          const item = items[0].toUpperCase();
          if (item.length === 6) {
            this.pnr.flights[
              this.quickInputIndex
            ].flight.departureAirport = item.substr(0, 3);
            this.pnr.flights[
              this.quickInputIndex
            ].flight.arrivalAirport = item.substr(3, 6);
          }
        }

        if (items.length > 1) {
          const item = items[1].toUpperCase();
          this.pnr.flights[this.quickInputIndex].flight.flightNo = item;
        }
        if (items.length > 2) {
          const item = items[2].toUpperCase();
          this.pnr.flights[this.quickInputIndex].flight.subclass = item;
        }
        if (items.length > 3) {
          const item = items[3].toUpperCase();
          this.pnr.flights[this.quickInputIndex].flight.departureDate = item;
        }
      }
    },
    selectAllPsg: function () {
      if (this.pnr === null) {
        return;
      }
      for (const psg of this.pnr.passengers) {
        psg.selected = true;
      }
    },
    unselectAllPsg: function () {
      if (this.pnr === null) {
        return;
      }
      for (const psg of this.pnr.passengers) {
        psg.selected = false;
      }
    },
  },
};
// 585
</script>